<template>
  <div class="ChildPage TablePage" v-loading="loading">
    <vb-page ref="page" @onTableSelection="doSelect">
      <template #headerBtn="scope">
        <el-upload
            action=""
            :accept="'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'"
            :before-upload="doUploadTemplate"
            :show-file-list="false">
          <template #trigger>
            <el-button size="mini" type="success" icon="el-icon-upload2" style="margin-left: 10px">上传销售订单</el-button>
          </template>
          <span></span>
        </el-upload>
      </template>
      <template #table-handle-prepend="scope">
        <el-button size="mini" type="text" icon="el-icon-edit" @click="gotoDetail(scope.row.billNo)">查看</el-button>
      </template>
      <template #table-stockNo="scope">
        {{ getStockName(scope.row.stockNo) }}
      </template>
      <template #table-count="scope">
        {{ scope.row.count? Number(scope.row.count.toFixed(6)): scope.row.count }}
      </template>
      <template #table-status="scope">
        <el-tag :type="scope.row.status === 'save'? 'info': 'success'">{{ scope.row.status === 'save'? '保存': '审核' }}</el-tag>
      </template>
      <template #footerBtn="data">
        <el-button type="primary" @click="doPurIn" icon="el-icon-s-home" size="mini">选择行出库</el-button>
      </template>
    </vb-page>
  </div>
</template>

<script>
export default {
  data() {
    return {
      materialLoading: false,
      reloadMaterial: false,
      selection: null
    }
  },
  async mounted() {
    await this.getStock()
  },
  methods: {
    doSelect(data) {
      this.selection = data
      console.log(data)
    },
    async doPurIn() {
      if (!this.selection || this.selection.length === 0) {
        this.$message.error('没有选择行')
        return
      }
      let billNoList = []
      for(let i = 0; i < this.selection.length; i++) {
        billNoList.push(this.selection[i].billNo)
      }
      try {
        await this.$confirm(`已审核单据(${ billNoList.length }件)将被执行销售出库, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
      } catch(e) {
        return
      }
      this.loading = true
      try {
        let res = await this.callApi('store_sale_bill_out_list', {
          billNoList: billNoList
        })
        if (!res.res) {
          return
        }
        this.$message.success(`出库完成!`)
        return true;
      } finally {
        this.loading = false
      }
    },
    async doUploadTemplate(file) {
      try {
        this.loading = true
        let res = await this.callApi("upload_store_sale_bill", { file })
        if (!res.res) return
        if (res.message) {
          this.$message.error(res.message)
        } else {
          this.$message.success('同步已完成')
        }
        return this.$refs.page.data.reload()
      } finally {
        this.loading = false
      }
    },
    async getStock() {
      this.loading = true;
      try {
        let res = await this.$api.getChildDictionaryListByKeys({
          keys: ['stock']
        });
        if (!res.res) return;
        this.stockList = res.stock;
      } finally {
        this.loading = false;
      }
    },
    /**
     * 获取状态名
     */
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for(let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name;
          }
        }
      }
      return '';
    },
    gotoDetail(billNo = '') {
      let url = '/sale/saleStoreSaleDetail'
      if (billNo) {
        url += `?billNo=${ billNo }`
      }
      this.$router.push(url)
    }
  }
}
</script>

<style scoped>

</style>
